home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5347 < prev    next >
Encoding:
Text File  |  1996-08-05  |  10.3 KB  |  422 lines

  1. Path: dragon.cs.hku.hk!kwho
  2. From: kwho@csd.hku.hk (Lord Hermes)
  3. Newsgroups: comp.lang.c++
  4. Subject: Problem with CBitmapButton::AutoLoad()
  5. Date: 3 Feb 1996 23:55:30 GMT
  6. Organization: Department of Computer Science, The University of Hong Kong
  7. Message-ID: <4f0sli$9ge@ns.cs.hku.hk>
  8. NNTP-Posting-Host: dragon.cs.hku.hk
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11. I'm new to VC++ programming and am currently using VC++ 2.0 under
  12. Windows 95...
  13.  
  14. I've encountered a strange problem with the method AutoLoad() of
  15. the CBitmapButton class, the method fails to locate the bitmap 
  16. file in my dialog-based application. I've traced the code with the
  17. debugger and find out the problem is that the program fails to get
  18. the bitmap when it gets to the point to retrieve resource CLASSU (
  19. see below, pls), which leads to an exeception... The bitmap files
  20. are stored in ".\res\" and the Visual Workbench can corretly locate
  21. it for me to edit the bitmap. (Actually I created them using the
  22. visual workbench) I'm following the method discribed in the book
  23. "Visual C++ Power Toolkit" by Richard C. Leinecker and Jamie Nye.
  24. It works for their tutorial exercise but...
  25.  
  26. If you happen to know what have I done wrong. May I ask u for some
  27. advice, pls? Thanx in advance. 
  28.  
  29. >=== SR.RC (Resource file -- truncated a bit) ===================<
  30.  
  31. //Microsoft Visual C++ generated resource script.
  32. //
  33. #include "resource.h"
  34.  
  35. #define APSTUDIO_READONLY_SYMBOLS
  36. /////////////////////////////////////////////////////////////////////////////
  37. //
  38. // Generated from the TEXTINCLUDE 2 resource.
  39. //
  40. #include "afxres.h"
  41.  
  42. /////////////////////////////////////////////////////////////////////////////
  43. #undef APSTUDIO_READONLY_SYMBOLS
  44.  
  45.  
  46. #ifdef APSTUDIO_INVOKED
  47. /////////////////////////////////////////////////////////////////////////////
  48. //
  49. // TEXTINCLUDE
  50. //
  51.  
  52. 1 TEXTINCLUDE DISCARDABLE 
  53. BEGIN
  54.     "resource.h\0"
  55. END
  56.  
  57. 2 TEXTINCLUDE DISCARDABLE 
  58. BEGIN
  59.     "#include ""afxres.h""\r\n"
  60.     "\0"
  61. END
  62.  
  63. 3 TEXTINCLUDE DISCARDABLE 
  64. BEGIN
  65.     "#include ""res\\SR.rc2""  // non-Microsoft Visual C++ edited resources\r\n"
  66.     "\r\n"
  67.     "#define _AFX_NO_SPLITTER_RESOURCES\r\n"
  68.     "#define _AFX_NO_OLE_RESOURCES\r\n"
  69.     "#define _AFX_NO_TRACKER_RESOURCES\r\n"
  70.     "#define _AFX_NO_PROPERTY_RESOURCES\r\n"
  71.     "#include ""afxres.rc""  // Standard components\r\n"
  72.     "\0"
  73. END
  74.  
  75. /////////////////////////////////////////////////////////////////////////////
  76. #endif    // APSTUDIO_INVOKED
  77.  
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. //
  81. // Icon
  82. //
  83.  
  84. IDR_MAINFRAME           ICON    DISCARDABLE     "res\\SR.ico"
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. //
  88. // Dialog
  89. //
  90.  
  91. IDD_ABOUTBOX DIALOG DISCARDABLE  34, 22, 217, 55
  92. STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
  93. CAPTION "About SR"
  94. FONT 8, "MS Sans Serif"
  95. BEGIN
  96.     ICON            IDR_MAINFRAME,IDC_STATIC,11,9,18,20
  97.     LTEXT           "SR Version 1.0",IDC_STATIC,40,10,119,8
  98.     LTEXT           "Copyright \251 1996",IDC_STATIC,40,25,119,8
  99.     DEFPUSHBUTTON   "OK",IDOK,176,6,32,14,WS_GROUP
  100. END
  101.  
  102. IDD_SR_DIALOG DIALOG DISCARDABLE  0, 0, 185, 92
  103. STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
  104. CAPTION "SR"
  105. FONT 8, "MS Sans Serif"
  106. BEGIN
  107.     DEFPUSHBUTTON   "OK",IDOK,129,6,50,14
  108.     PUSHBUTTON      "Cancel",IDCANCEL,129,23,50,14
  109.     CONTROL         "CLASS",IDC_CLASS,"Button",BS_OWNERDRAW | WS_TABSTOP,9,6,
  110.                     49,14
  111. END
  112.  
  113.  
  114. /////////////////////////////////////////////////////////////////////////////
  115. //
  116. // Version
  117. //
  118.  
  119. VS_VERSION_INFO VERSIONINFO
  120.  FILEVERSION 1,0,0,1
  121.  PRODUCTVERSION 1,0,0,1
  122.  FILEFLAGSMASK 0x3fL
  123. #ifdef _DEBUG
  124.  FILEFLAGS 0x1L
  125. #else
  126.  FILEFLAGS 0x0L
  127. #endif
  128.  FILEOS 0x4L
  129.  FILETYPE 0x1L
  130.  FILESUBTYPE 0x0L
  131. BEGIN
  132.     BLOCK "StringFileInfo"
  133.     BEGIN
  134.         BLOCK "040904B0"
  135.         BEGIN
  136.             VALUE "CompanyName", "\0"
  137.             VALUE "FileDescription", "SR MFC Application\0"
  138.             VALUE "FileVersion", "1, 0, 0, 1\0"
  139.             VALUE "InternalName", "SR\0"
  140.             VALUE "LegalCopyright", "Copyright \251 1996\0"
  141.             VALUE "LegalTrademarks", "\0"
  142.             VALUE "OriginalFilename", "SR.EXE\0"
  143.             VALUE "ProductName", "SR Application\0"
  144.             VALUE "ProductVersion", "1, 0, 0, 1\0"
  145.         END
  146.     END
  147.     BLOCK "VarFileInfo"
  148.     BEGIN
  149.         VALUE "Translation", 0x409, 1200
  150.     END
  151. END
  152.  
  153.  
  154.  
  155. /////////////////////////////////////////////////////////////////////////////
  156. //
  157. // Bitmap
  158. //
  159.  
  160. CLASSD                  BITMAP  DISCARDABLE     "res\\classd.bmp"
  161. CLASSF                  BITMAP  DISCARDABLE     "res\\classf.bmp"
  162. CLASSU                  BITMAP  DISCARDABLE     "res\\classu.bmp"
  163. CLASSX                  BITMAP  DISCARDABLE     "res\\classx.bmp"
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. //
  167. // String Table
  168. //
  169.  
  170. STRINGTABLE DISCARDABLE 
  171. BEGIN
  172.     ID_INDICATOR_EXT        "EXT"
  173.     ID_INDICATOR_CAPS       "CAP"
  174.     ID_INDICATOR_NUM        "NUM"
  175.     ID_INDICATOR_SCRL       "SCRL"
  176.     ID_INDICATOR_OVR        "OVR"
  177.     ID_INDICATOR_REC        "REC"
  178. END
  179.  
  180. STRINGTABLE DISCARDABLE 
  181. BEGIN
  182.     IDS_ABOUTBOX            "&About SR..."
  183. END
  184.  
  185.  
  186. #ifndef APSTUDIO_INVOKED
  187. /////////////////////////////////////////////////////////////////////////////
  188. //
  189. // Generated from the TEXTINCLUDE 3 resource.
  190. //
  191. #include "res\SR.rc2"  // non-Microsoft Visual C++ edited resources
  192.  
  193. #define _AFX_NO_SPLITTER_RESOURCES
  194. #define _AFX_NO_OLE_RESOURCES
  195. #define _AFX_NO_TRACKER_RESOURCES
  196. #define _AFX_NO_PROPERTY_RESOURCES
  197. #include "afxres.rc"  // Standard components
  198.  
  199. /////////////////////////////////////////////////////////////////////////////
  200. #endif    // not APSTUDIO_INVOKED
  201.  
  202. >========== SRdlg.h (definition of the dialog window) ================<
  203.  
  204. // SRdlg.h : header file
  205. //
  206.  
  207. /////////////////////////////////////////////////////////////////////////////
  208. // CSRDlg dialog
  209.  
  210. class CSRDlg : public CDialog
  211. {
  212. // Construction
  213. public:
  214.     CSRDlg(CWnd* pParent = NULL);    // standard constructor
  215.  
  216. // Dialog Data
  217.     //{{AFX_DATA(CSRDlg)
  218.     enum { IDD = IDD_SR_DIALOG };
  219.         // NOTE: the ClassWizard will add data members here
  220.     //}}AFX_DATA
  221.  
  222.     // ClassWizard generated virtual function overrides
  223.     //{{AFX_VIRTUAL(CSRDlg)
  224.     protected:
  225.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  226.     //}}AFX_VIRTUAL
  227.  
  228. // Attributes
  229. protected:
  230.     CBitmapButton m_class;
  231.  
  232. // Implementation
  233. protected:
  234.     HICON m_hIcon;
  235.  
  236.     // Generated message map functions
  237.     //{{AFX_MSG(CSRDlg)
  238.     virtual BOOL OnInitDialog();
  239.     afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
  240.     afx_msg void OnPaint();
  241.     afx_msg HCURSOR OnQueryDragIcon();
  242.     //}}AFX_MSG
  243.     DECLARE_MESSAGE_MAP()
  244. };
  245.  
  246.  
  247. >============ SRdlg.CPP (the implementation of the dialog) ========<
  248.  
  249. // SRdlg.cpp : implementation file
  250. //
  251.  
  252. #include "stdafx.h"
  253. #include "SR.h"
  254. #include "SRdlg.h"
  255.  
  256. #ifdef _DEBUG
  257. #undef THIS_FILE
  258. static char BASED_CODE THIS_FILE[] = __FILE__;
  259. #endif
  260.  
  261. /////////////////////////////////////////////////////////////////////////////
  262. // CAboutDlg dialog used for App About
  263.  
  264. class CAboutDlg : public CDialog
  265. {
  266. public:
  267.     CAboutDlg();
  268.  
  269. // Dialog Data
  270.     //{{AFX_DATA(CAboutDlg)
  271.     enum { IDD = IDD_ABOUTBOX };
  272.     //}}AFX_DATA
  273.  
  274. // Implementation
  275. protected:
  276.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  277.     //{{AFX_MSG(CAboutDlg)
  278.     virtual BOOL OnInitDialog();
  279.     //}}AFX_MSG
  280.     DECLARE_MESSAGE_MAP()
  281. };
  282.  
  283. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  284. {
  285.     //{{AFX_DATA_INIT(CAboutDlg)
  286.     //}}AFX_DATA_INIT
  287. }
  288.  
  289. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  290. {
  291.     CDialog::DoDataExchange(pDX);
  292.     //{{AFX_DATA_MAP(CAboutDlg)
  293.     //}}AFX_DATA_MAP
  294. }
  295.  
  296. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  297.     //{{AFX_MSG_MAP(CAboutDlg)
  298.         // No message handlers
  299.     //}}AFX_MSG_MAP
  300. END_MESSAGE_MAP()
  301.  
  302. /////////////////////////////////////////////////////////////////////////////
  303. // CAboutDlg message handlers
  304.  
  305. BOOL CAboutDlg::OnInitDialog()
  306. {
  307.     CDialog::OnInitDialog();
  308.     CenterWindow();
  309.     
  310.     // TODO: Add extra about dlg initialization here
  311.     
  312.     return TRUE;  // return TRUE  unless you set the focus to a control
  313. }
  314.  
  315. /////////////////////////////////////////////////////////////////////////////
  316. // CSRDlg dialog
  317.  
  318. NULL*/)
  319.     : CDialog(CSRDlg::IDD, pParent)
  320. {
  321.     //{{AFX_DATA_INIT(CSRDlg)
  322.         // NOTE: the ClassWizard will add member initialization here
  323.     //}}AFX_DATA_INIT
  324.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  325.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  326. }
  327.  
  328. void CSRDlg::DoDataExchange(CDataExchange* pDX)
  329. {
  330.     CDialog::DoDataExchange(pDX);
  331.     //{{AFX_DATA_MAP(CSRDlg)
  332.         // NOTE: the ClassWizard will add DDX and DDV calls here
  333.     //}}AFX_DATA_MAP
  334. }
  335.  
  336. BEGIN_MESSAGE_MAP(CSRDlg, CDialog)
  337.     //{{AFX_MSG_MAP(CSRDlg)
  338.     ON_WM_SYSCOMMAND()
  339.     ON_WM_PAINT()
  340.     ON_WM_QUERYDRAGICON()
  341.     //}}AFX_MSG_MAP
  342. END_MESSAGE_MAP()
  343.  
  344. /////////////////////////////////////////////////////////////////////////////
  345. // CSRDlg message handlers
  346.  
  347. BOOL CSRDlg::OnInitDialog()
  348. {
  349.     CDialog::OnInitDialog();
  350.     CenterWindow();
  351.  
  352.     // Add "About..." menu item to system menu.
  353.  
  354.     // IDM_ABOUTBOX must be in the system command range.
  355.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  356.     ASSERT(IDM_ABOUTBOX < 0xF000);
  357.  
  358.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  359.     CString strAboutMenu;
  360.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  361.     if (!strAboutMenu.IsEmpty())
  362.     {
  363.         pSysMenu->AppendMenu(MF_SEPARATOR);
  364.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  365.     }
  366.     
  367.     // TODO: Add extra initialization here
  368.     VERIFY(m_class.AutoLoad(IDC_CLASS, this));
  369.     
  370.     return TRUE;  // return TRUE  unless you set the focus to a control
  371. }
  372.  
  373. void CSRDlg::OnSysCommand(UINT nID, LPARAM lParam)
  374. {
  375.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  376.     {
  377.         CAboutDlg dlgAbout;
  378.         dlgAbout.DoModal();
  379.     }
  380.     else
  381.     {
  382.         CDialog::OnSysCommand(nID, lParam);
  383.     }
  384. }
  385.  
  386. // If you add a minimize button to your dialog, you will need the code below
  387. //  to draw the icon.  For MFC applications using the document/view model,
  388. //  this is automatically done for you by the framework.
  389.  
  390. void CSRDlg::OnPaint() 
  391. {
  392.     if (IsIconic())
  393.     {
  394.         CPaintDC dc(this); // device context for painting
  395.  
  396.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  397.  
  398.         // Center icon in client rectangle
  399.         int cxIcon = GetSystemMetrics(SM_CXICON);
  400.         int cyIcon = GetSystemMetrics(SM_CYICON);
  401.         CRect rect;
  402.         GetClientRect(&rect);
  403.         int x = (rect.Width() - cxIcon + 1) / 2;
  404.         int y = (rect.Height() - cyIcon + 1) / 2;
  405.  
  406.         // Draw the icon
  407.         dc.DrawIcon(x, y, m_hIcon);
  408.     }
  409.     else
  410.     {
  411.         CDialog::OnPaint();
  412.     }
  413. }
  414.  
  415. // The system calls this to obtain the cursor to display while the user drags
  416. //  the minimized window.
  417. HCURSOR CSRDlg::OnQueryDragIcon()
  418. {
  419.     return (HCURSOR) m_hIcon;
  420. }
  421.  
  422.